#include <all_wlib.h>
#pragma hdrstop
//#pragma package(smart_init)
#pragma resource "*.dfm"
#pragma link "HGrid"

#include "ghed_int.h"
#include "ghe_TextEdit.h"

LOCALSTRUCT( TextInfo )
  DWORD    off;
  CONSTSTR Name;
};
static TextInfo KnownTexts[] = {
  { 0x39000, "0x39000 - English" },
  { 0x3A200, "0x3A200 - Spanish" },
  { 0x3B400, "0x3B400 - Italiano" },
  { 0x3C600, "0x3C600 - French" },
  { 0x3D800, "0x3D800 - Deutch" }
};

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
__fastcall TTextEditForm::TTextEditForm(TComponent* Owner)
    : TForm(Owner)
  {
     open->InitialDir = GetStartupDir(NULL);
     grid->ItemsCount = 0;

     for( int n = 0; n < ARRAY_SIZE(KnownTexts); n++ )
       edtOff->Items->Add( KnownTexts[n].Name );
     edtOff->ItemIndex = 0;
     edtOffClick(NULL);

     RefreshText();
}
void TTextEditForm::RefreshText( void )
  {
     DWORD cn = Strings.CalcSize();
     status->Panels->Items[1]->Text = Message( "%02X / %02X (%d)",
                                               cn, sizeof(dump.Text),
                                               sizeof(dump.Text)-cn );
}

//---------------------------------------------------------------------------
void __fastcall TTextEditForm::btnImportClick(TObject *Sender)
  {
    if ( Strings.Count() &&
         !AskYesNo( Handle, NULL, "Strings contains data.\nImport new data anyway?" ) )
      return;

    __try{
      if ( !Strings.Load( edtFile->Text.c_str(),
                          Str2DigitDetect( StrGetCol(edtOff->Text.c_str(),1," "), 10, 0 ) ) ) {
        AskMessage( Handle, NULL,
                    "Error loading data.\nFile: %s\nError: %s",
                    edtFile->Text.c_str(), FIO_ERROR );
        return;
      }
    }__finally{
      grid->ItemsCount = Strings.Count();
      grid->Invalidate();
      RefreshText();
    }
}
//---------------------------------------------------------------------------
void __fastcall TTextEditForm::gridHOnGetCellText(TObject *Sender, int nX,int nY, AnsiString &Value)
  {  PTextItem p = Strings.Item(nY);
     if ( !p ) return;

     switch( nX ) {
       case 0: Value.printf( "%d", nY ); break;
       case 1: Value.printf( "%02X", p->off ); break;
       case 2: Value.printf( "%d (%02X)", p->text.Length(), p->text.Length() ); break;
       case 3: Value = p->orig; break;
       case 4: Value = p->text; break;
     }
}
//---------------------------------------------------------------------------
void __fastcall TTextEditForm::gridHOnSetCellText(TObject *Sender, int nX,int nY, const AnsiString &Value)
  {  if ( nX != 4 ) return;
     PTextItem p = Strings.Item(nY);
     if ( !p ) return;

     p->text = ConvertedText( Value.c_str() );
     RefreshText();
}
//---------------------------------------------------------------------------
void __fastcall TTextEditForm::btnExportClick(TObject *Sender)
  {
     if ( !Strings.Count() )
       return;

     DWORD cn = Strings.CalcSize();
     if ( cn >= sizeof(dump.Text) ) {
       AskMessage( Handle, NULL,
                   "Impossible to store strings to file.\nText size exceed maximum.\nEntered %d bytes, expected %d bytes",
                   cn, sizeof(dump.Text) );
       return;
     }

     if ( !Strings.Save( edtFile->Text.c_str(),
                         Str2DigitDetect( StrGetCol(edtOff->Text.c_str(),1," "), 10, 0 ) ) ) {
       AskMessage( Handle, NULL,
                   "Error store data.\nFile: %s\nError: %s",
                   edtFile->Text.c_str(), FIO_ERROR );
       return;
     }

     AskMessage( Handle, NULL,
                 "File \"%s\" was successfully updated!",
                 edtFile->Text.c_str() );
}
//---------------------------------------------------------------------------
void __fastcall TTextEditForm::gridKeyDown(TObject *Sender, WORD &Key,TShiftState Shift)
  {
     if ( Key == 'F' && Shift.Contains(ssCtrl) ) {
       static AnsiString s;
       if ( !HIputDialog( this, "Looking for text", "Enter text to search...", s ) )
         return;

       AnsiString ss( s );
       StrUpr( ss.c_str() );
       ss = ConvertedText( ss.c_str() );
       for( int n = Max( 0, grid->ItemIndex ); n < Strings.Count(); n++ )
         if ( StrStr( Strings[n]->text.c_str(), ss.c_str(), FALSE ) != NULL ) {
           grid->ItemIndex = n;
           return;
         }
     }
}
//---------------------------------------------------------------------------
void __fastcall TTextEditForm::edtOffClick(TObject *Sender)
  {  int n = edtOff->ItemIndex;
     if ( n >= 0 && n < ARRAY_SIZE(KnownTexts) )
       edtOff->Text = Message( "0x%08X", KnownTexts[n].off );
}
//---------------------------------------------------------------------------
void __fastcall TTextEditForm::btnOpenClick(TObject *Sender)
  {
     if ( !open->Execute() ) return;
     edtFile->Text = open->FileName;
}
//---------------------------------------------------------------------------

